package com.skyinno.mrms.utils;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.util.WebUtils;

public class LocaleInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        Locale locale = (Locale) WebUtils.getSessionAttribute(arg0,
                SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
        if (locale == null) {
            locale = arg0.getLocale();
        }
        if (!Locale.SIMPLIFIED_CHINESE.equals(locale)
                && !Locale.ENGLISH.equals(locale)) {
            locale = Locale.SIMPLIFIED_CHINESE;
        }
        WebUtils.setSessionAttribute(arg0,
                SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
        return super.preHandle(arg0, arg1, arg2);
    }

}
